//
//  InvestListCell.swift
//  cxd4iphone
//
//  Created by hexy on 12/1/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

class InvestListCell: BaseTableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        selectionStyle = .None
    }

    override func updateUI(viewModel: BaseCellViewModel) {
        
        let vm = viewModel as! InvestCellViewModel
        indentBtn.setImage(vm.indentImage, forState: .Normal)
        titleLabel.text = vm.bidModel.loanPurpose!
        timeLabel.text = vm.releaseTime
        totalInvestNumLabel.text = vm.bidModel.loanMoney!
        availableNumLabel.text = vm.bidModel.remainMoney!
        cpView.investCellViewModel = vm
        investTimeNumLabel.text = vm.investTimeNum
        investTimeUnitLabel.text = vm.investTimeUnit
        
        rateNumLabel.text = vm.bidModel.rate
//        investTimeLabel.text = vm.investTime
        cpView.enabled = vm.cpViewEnable
        cpView.setTitle(vm.cpViewTitle, forState: vm.cpViewState)
        
        xx_print(vm.bidModel.status)
    }
    
    

    
    override func makeUI() {
        self.accessoryType = .None
        self.backgroundColor = UIColor.whiteColor()
        
        // 顶部分割线
        let divideView = UIView(bgColor: Define.backgroundColor)
        
        contentView.addSubview(divideView)
        divideView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(contentView)
            make.height.equalTo(xx_height(10))
        }
        
        
        // 顶部
//        let topView = UIView(bgColor: xx_randomColor())
        let topView = UIView()
        
        contentView.addSubview(topView)
        
        topView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(divideView.snp_bottom)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView).offset(-15)
            make.height.equalTo(xx_height(30))
        }
        
        // 标识服
        topView.addSubview(indentBtn)
        indentBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.left.bottom.equalTo(topView)
        }
//
//        // 产品名称
        topView.addSubview(titleLabel)
//        titleLabel.backgroundColor = xx_randomColor()
        titleLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(indentBtn.snp_right)
            make.centerY.equalTo(topView)
        }
//
//        // 发布时间
        contentView.addSubview(timeLabel)
//        timeLabel.backgroundColor = xx_randomColor()
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(topView)
            make.centerY.equalTo(titleLabel)
        }
        
//        let midView = UIView(bgColor: xx_randomColor())
        let midView = UIView()
        
        contentView.addSubview(midView)
        midView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(topView.snp_bottom)
            make.left.equalTo(contentView).offset(15)
            make.right.equalTo(contentView).offset(-15)
            make.height.equalTo(xx_height(80))
        }
        
        midView.addSubview(cpView)
        cpView.snp_makeConstraints { (make) -> Void in
            
            make.right.centerY.equalTo(midView)
            make.width.height.equalTo(xx_height(60))
        }
        
//        let rateWarp = UIView(bgColor: xx_randomColor())
        let rateWarp = UIView()
        
        
        
        midView.addSubview(rateWarp)
        rateWarp.snp_makeConstraints { (make) -> Void in
            
            make.left.centerY.equalTo(midView)
            make.height.equalTo(cpView)
            make.width.equalTo(xx_width(100))
        }
        
        rateWarp.addSubview(rateNumLabel)
//        rateNumLabel.backgroundColor = xx_randomColor()
        rateNumLabel.snp_makeConstraints { (make) -> Void in
            
            let marginTop = xx_iPhone4() ? xx_height(-8) : 0

            make.top.equalTo(rateWarp).offset(marginTop)
            make.left.equalTo(rateWarp)
            
        }
        
        rateWarp.addSubview(percentLabel)
//        percentLabel.backgroundColor = xx_randomColor()
        percentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(rateNumLabel.snp_right)
            
            make.centerY.equalTo(rateNumLabel).offset(xx_folat(13))

        }
        
//        let investTimeWrap = UIView(bgColor: xx_randomColor())
                let investTimeWrap = UIView()
        
        midView.addSubview(investTimeWrap)
        investTimeWrap.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(midView)
            make.bottom.equalTo(cpView)
//            make.width.equalTo(50)
//            make.height.equalTo(30)
        }
        
        investTimeWrap.addSubview(investTimeNumLabel)
//        investTimeNumLabel.backgroundColor = xx_randomColor()
        investTimeNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.left.bottom.equalTo(investTimeWrap)
            
        }
        
        investTimeWrap.addSubview(investTimeUnitLabel)
        investTimeUnitLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(investTimeNumLabel.snp_right)
            make.centerY.equalTo(investTimeNumLabel)
            make.right.equalTo(investTimeWrap)
        }
        
        
//
//        // 底部
        let bottomView = UIView()
//        bottomView.backgroundColor = xx_randomColor()
        
        contentView.addSubview(bottomView)
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(midView.snp_bottom)
            make.left.right.equalTo(midView)
            make.height.equalTo(xx_height(30))
        }
//
        let hLine = UIView(bgColor: Define.separatorColor)
        
        bottomView.addSubview(hLine)
        hLine.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(bottomView)
            make.height.equalTo(1)
        }
//
        let vLine = UIView(bgColor:  Define.separatorColor)
        
        bottomView.addSubview(vLine)
        vLine.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(bottomView)
            make.height.equalTo(xx_height(16))
            make.width.equalTo(1)
        }
//
        let totalLabel = makeFormatView("项目总额", leftFontSize: 12, rightString: " (元)", rightFontSize: 9, texColor:  Define.darkFontColor)
        
        bottomView.addSubview(totalLabel)
        totalLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.centerY.equalTo(bottomView)
        }
//
        bottomView.addSubview(totalInvestNumLabel)
        totalInvestNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(totalLabel.snp_right).offset(3)
            make.centerY.equalTo(totalLabel)
        }
//
        bottomView.addSubview(availableNumLabel)
        availableNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(bottomView)
            make.centerY.equalTo(totalLabel)
        }
        
        let availableLabel = makeFormatView("可投金额", leftFontSize: 12, rightString: " (元)", rightFontSize: 9, texColor:  Define.darkFontColor)
        
        bottomView.addSubview(availableLabel)
        availableLabel.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(availableNumLabel.snp_left).offset(-3)
            make.centerY.equalTo(totalLabel)
        }

        
    }
    
    lazy var investTimeNumLabel = UILabel(num: "8", fontSize: 18)
    lazy var investTimeUnitLabel = UILabel(num: "天", fontSize: 14)
    lazy var cpView: CircleProgressView = CircleProgressView()
    
     /// 投资标识
    private lazy var indentBtn = UIButton()
    private lazy var titleLabel = UILabel(num: "房屋抵押BJ1508021-10", fontSize: 15)
    private lazy var timeLabel = UILabel(title: "发布时间:2015-11-12", fontSize: 10)
    
    private lazy var totalInvestNumLabel = UILabel(num: "5,500,000", fontSize: 15)
    private lazy var availableNumLabel = UILabel(num: "1,500,000",  fontSize: 15)

    
    lazy var rateNumLabel = UILabel(num: "20", fontSize: 65, fontColor: Define.redColor)
    lazy var percentLabel = UILabel(num: "%", fontSize: 25, fontColor: Define.redColor)
    
    private func makeFormatView(leftString: String, leftFontSize: CGFloat, rightString: String, rightFontSize:CGFloat, texColor: UIColor) -> UIView {
        
        let format = UIView()
        
        let leftLabel = UILabel(title: leftString, fontColor: texColor, bgColor: nil, fontSize: leftFontSize, maxWrapWidth: 0)
        
        format.addSubview(leftLabel)
        leftLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.left.bottom.equalTo(format)
        }
        let rightLabel = UILabel(title: rightString, fontColor: texColor, bgColor: nil, fontSize: rightFontSize, maxWrapWidth: 0)
        
        format.addSubview(rightLabel)
        rightLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(leftLabel.snp_right)
            make.bottom.equalTo(format).offset(-2)
            make.right.equalTo(format)
        }
        return format
    }
    
    
}
